<?php

namespace app\admin\controller;

use app\admin\controller\AdminController;
use think\Request;
use think\Db;

class Column extends AdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::name('column')->order('column_id')->paginate(3);
        return view('column/index',[
            'title' => '节点列表',
            'list' => $data
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('column/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $req = $request->post();
        $data = [
            'colname' => $req['colname']
        ];
        $status = Db::name('column')->where('colname','=',$data['colname'])->select();
        if(!empty($status)){
            echo "<script>alert('添加失败,栏目已经存在');window.history.back();</script>";
        }else{
            $data = Db::name('column')->insert($data);
            if ($data > 0){
                return "<script>alert('添加成功');window.history.back()</script>";
            }else {
                return "<script>alert('添加失败');window.history.back()</script>";
            }
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        $edit = Db::name('column')->where('column_id',$_POST['id'])->select();
        foreach($edit as $v){

                $data = [
                    'column_id'=>$v['column_id'],
                    'colname'=>$v['colname']
                ];
        }

        return json($data);
        // return view('column/update',[
        //     'title' => '修改栏目',
        //     'list' => $edit
        // ]);

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {

        $data = [
            'colname' => $_POST['colname']
        ];
        $update = Db::name('column')->where('column_id', $_POST['id'])->update($data);

        if ($update > 0){
            $info['status'] = true;
            $info['info'] = 'ID为: ' . $_POST['id'] . '的栏目更新成功!';
        }else {
            $info['status'] = false;
            $info['info'] = 'ID为: ' . $_POST['id'] . '的栏目更新失败!';
        }
        return json($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $data = Db::name('column')->delete($id);
        if ($data > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的栏目删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的栏目删除失败,请重试!';
        }
        return json($info);
    }
}
